﻿using HCQ2_Common;
using HCQ2_Common.SQL;
using HCQ2_Model.VRDD.OutputDto._20220627;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HCQ2_DAL_MSSQL
{
    public partial class T_VRScoreSourceDetailDAL
    {
        /// <summary>
        ///  获取用户签到积分
        /// </summary>
        /// <returns></returns>
        public decimal GetUserSignsCore()
        {
            sb?.Clear();
            sb.Append(@"SELECT CAST(ISNULL(m.numValue,0) AS DECIMAL(6,2)) FROM dbo.T_ItemCodeMenum m WHERE EXISTS(
		    SELECT item_id FROM dbo.T_ItemCode WHERE item_code='PointsRule' 
		    AND item_id=m.item_id) AND m.code_value='UserSign';");
            return Helper.ToDecimal(SqlHelper.ExecuteScalar(sb.ToString()));
        }
        /// <summary>
        ///  获取用户连续签到天数奖励
        /// </summary>
        /// <param name="day"></param>
        /// <returns></returns>
        public decimal GetUserSignsAwardCore(int day)
        {
            sb?.Clear();
            sb.Append($@"SELECT CAST(ISNULL(m.numValue,0) AS DECIMAL(6,2)) FROM dbo.T_ItemCodeMenum m WHERE EXISTS(
		    SELECT item_id FROM dbo.T_ItemCode WHERE item_code='SignRewardRule' 
		    AND item_id=m.item_id) AND m.code_value='{day}'");
            return SqlHelper.ExecuteScalarInt32(sb.ToString());
        }
        /// <summary>
        ///  获取签到奖励最大记录
        /// </summary>
        /// <returns></returns>
        public MaxSigDayScore GetMaxSigDayScore()
        {
            sb?.Clear();
            sb.Append(@"SELECT TOP 1 ISNULL(m.code_value,0) AS sigDay,
            CAST(ISNULL(m.numValue,0) AS DECIMAL(6,2)) AS numValue FROM dbo.T_ItemCodeMenum m WHERE EXISTS(
		    SELECT item_id FROM dbo.T_ItemCode WHERE item_code='SignRewardRule' 
		    AND item_id=m.item_id) ORDER BY m.code_value DESC;");
            return SqlHelper.ExecuteList<MaxSigDayScore>(sb.ToString())?.FirstOrDefault();
        }
        /// <summary>
        ///  获取积分规则指定代码奖励
        /// </summary>
        /// <param name="codeValue"></param>
        /// <returns></returns>
        public AwardModel GetAwardByCode(string codeValue)
        {
            sb?.Clear();
            sb.Append($@"SELECT m.code_type AS whereValue,m.numValue FROM dbo.T_ItemCodeMenum m WHERE EXISTS(
            SELECT item_id FROM dbo.T_ItemCode WHERE item_code='PointsRule' AND item_id=m.item_id)
            AND m.code_value='{codeValue}';");
            return SqlHelper.ExecuteList<AwardModel>(sb.ToString())?.FirstOrDefault();
        }
    }
}